<template>
	<view class="login">
		<!-- 支付宝自定义导航栏 -->
		<CustomBar />
		<u-toast ref="uToast"></u-toast>
		<view class="content">
			<view class="title">
				欢迎登录智选骑行
			</view>
			<u-button type="primary" shape="circle" @click="login" class="loginBtn">一键登录</u-button>
			<!-- <u-button type="primary" shape="circle" plain :hairline="false">使用短信验证码登录</u-button> -->
		</view>

	</view>
</template>

<script>
	import request from '../../api/index.js'
	export default {
		name: 'login',
		onLoad() {
			let pages = getCurrentPages();
			console.log(pages)
			if (pages.length > 1) {
				let path = pages[pages.length - 2]
				console.log(path)
				this.redirectPath = path.$page.fullPath.split('?')[0];
				this.options = path.options
			}
		},
		data() {
			return {
				redirectPath: '',
				options: {}
			}
		},
		mounted() {
			this.$refs.uToast.show({
				type: 'default',
				message: '未登录,请先登录',
			})
		},
		methods: {
			login() {
				// #ifdef MP-ALIPAY
				my.getAuthCode({
					scopes: 'auth_user',
					success: res => {
						request('/user/login', {
							authCode: res.authCode
						}).then(res => {
							console.log(res)
							if (res.status === 0) {
								const that = this
								this.$store.commit('setUserInfo', res.data.userInfo)
								this.$store.commit('setToken', res.data.token)
								this.$refs.uToast.show({
									type: 'default',
									message: '登陆成功',
									complete() {
										// uni.navigateTo({
										// 	url: `${that.redirectPath}?bikeId=${that.options.bikeId}&&bikeType=${that.options.bikeType}`
										// })
										uni.navigateBack()
									}
								})
							}
						})
					}
				})
				// #endif
				// #ifdef MP-WEIXIN
				uni.login({
					success: (res) => {
						if (res.code) {
							request('/user/wxLogin', {
								code: res.code
							}).then(res => {
								console.log(res)
								if (res.status === 0) {
									const that = this
									this.$store.commit('setUserInfo', res.data.userInfo)
									this.$store.commit('setToken', res.data.token)
									this.$refs.uToast.show({
										type: 'default',
										message: '登陆成功',
										complete() {
											// uni.navigateTo({
											// 	url: `${that.redirectPath}?bikeId=${that.options.bikeId}&&bikeType=${that.options.bikeType}`
											// })
											uni.navigateBack()
										}
									})
								}
							})
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					},
					fail: (res) => {
						console.log(res, 'loginErr')
					},
				})
				// #endif
			}
		}
	}
</script>

<style scoped lang="scss">
	.login {
		height: 100vh;
		background: linear-gradient(#CFEAFF, #F6F6F6);

		.content {
			padding: 0 80rpx;
			text-align: center;

			.title {
				font-size: 40rpx;
				line-height: 1.5;
				font-weight: 600;
				margin-top: 200rpx;
				margin-bottom: 70rpx;
			}

			::v-deep.u-button {
				margin-bottom: 40rpx;
			}
		}
	}
</style>
